//
//  HMHomeTableViewCell.swift
//  WeiBo
//
//  Created by Feng on 2017/6/8.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit
import SnapKit

class HMHomeTableViewCell: UITableViewCell {

    //记录已经加载的toolBar的顶部约束
    var toolBarTopConstraint: Constraint?
    
    //调用set方法给属性赋值
    var statusCellViewModel: HMStatusCellViewModel?
    {
        didSet {
            //设置originalView 的viewModel
            originalView.statusCellViewModel = statusCellViewModel
            
            
            //卸载之前toolsBar的top约束
            toolBarTopConstraint?.deactivate()
            //设置toolBarView 的viewModel
            toolBarView.statusCellViewModel = statusCellViewModel
            
            //判断是否有转发微博
            if statusCellViewModel?.status?.retweeted_status?.text != nil
            {
                //有转发微博
                //1.设置不隐藏
                retweetView.isHidden = false
                //2.重新创建约束
                toolBarView.snp.makeConstraints({ (make) in
                    //重新赋值给toolBarTopConstraint?
                    toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
                })
                //3.设置viewModel
                retweetView.statusCellViewModel = statusCellViewModel
            }
            else
            {
                retweetView.isHidden = true
                toolBarView.snp.makeConstraints({ (make) in
                    toolBarTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })
                
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //设置ui
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: --加载控件
    private lazy var originalView: HMOriginalView = HMOriginalView()
    
    private lazy var retweetView: HMRetweetView = HMRetweetView()
    
    private lazy var toolBarView: HMToolBarView = HMToolBarView()
    
    //MARK: --设置界面约束
    func setupUI()
    {
        //设置背景色
        contentView.backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBarView)
        
        originalView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.leading.trailing.equalTo(contentView)
        }
        
        retweetView.snp.makeConstraints { (make) in
            make.top.equalTo(originalView.snp.bottom)
            make.leading.trailing.equalTo(originalView)
        }
        
        toolBarView.snp.makeConstraints { (make) in
            //记录约束对象
            toolBarTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.leading.trailing.equalTo(retweetView)
            make.bottom.equalTo(contentView)
        }
        
//        contentView.layer.drawsAsynchronously = true
//        contentView.layer.shouldRasterize = true
//        contentView.layer.rasterizationScale = UIScreen.main.scale
    }

}
